package com.pig4cloud.commcon.websocket;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket // 开启 websocket
public class WebSocketConfig implements WebSocketConfigurer {

	@Resource
	private MyWebSocketHandler webSocketHandler;

	@Resource
	private WebSocketInterceptor webSocketInterceptor;

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {

		webSocketHandlerRegistry.addHandler(webSocketHandler, "/ws/{token}")
                // 指定自定义拦截器
				.addInterceptors(webSocketInterceptor)
				// 允许跨域
				.setAllowedOrigins("*");
	}
}